package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.info.ConsumeInfo;
import com.sphd.beautysalon.presenter.VipMationPresenterImpl;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/11/10
 */

public class VipMationModelImpl implements VipMationModel, VolleyJsonObjectCallBack {
    VipMationPresenterImpl vipMationPresenter;

    @Override
    public void setUrl(String url, VipMationPresenterImpl vipMationPresenter) {
        this.vipMationPresenter = vipMationPresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void sendEditUrl(String s, VipMationPresenterImpl vipMationPresenter) {
        this.vipMationPresenter = vipMationPresenter;
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res =response.getInt("res");
                    vipMationPresenter.sendEditRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onErrorResponse(VolleyError error) {
            vipMationPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            double account = response.getDouble("account");
            int cards = response.getInt("cards");
            int integral = response.getInt("integral");
            List<ConsumeInfo> consumeInfos = JsonUtil.getListFromString(response.getString("orders"), ConsumeInfo.class);
            vipMationPresenter.sendRes(account,cards,integral,consumeInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        vipMationPresenter.sendError(error);
    }
}